package com.ruoyi.common.security.config;

import cn.hutool.core.date.DatePattern;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
import org.noear.solon.annotation.Bean;
import org.noear.solon.annotation.Configuration;
import org.noear.solon.annotation.Inject;
import org.noear.solon.serialization.jackson.JacksonActionExecutor;
import org.noear.solon.serialization.jackson.JacksonRenderFactory;

import java.time.LocalDate;
import java.time.LocalTime;

/**
 * Jackson配置
 * @author ruoyi
 */
@Configuration
public class JacksonConfig {

    /**
     * 序列化
     * @param factory
     * @param executor
     */
    @Bean
    public void jsonInit(@Inject JacksonRenderFactory factory, @Inject JacksonActionExecutor executor) {
        factory.addEncoder(LocalDate.class, new LocalDateSerializer(DatePattern.NORM_DATE_FORMATTER));
        executor.addDeserializer(LocalDate.class, new LocalDateDeserializer(DatePattern.NORM_DATE_FORMATTER));

        factory.addEncoder(LocalTime.class, new LocalTimeSerializer(DatePattern.NORM_TIME_FORMATTER));
        executor.addDeserializer(LocalTime.class, new LocalTimeDeserializer(DatePattern.NORM_TIME_FORMATTER));
    }

}
